package com.tairanchina.md.meeting.filter;

import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.ext.Provider;

import com.tairanchina.md.meeting.constants.MeetingConstants;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tairanchina.beego.api.exception.AuthenticateException;
import com.tairanchina.beego.api.model.BeegoToken;
import com.tairanchina.md.meeting.constants.UserCommonField;
import com.tairanchina.txRestFilter.TxRestFilter;
import com.tairanchina.txRestFilter.configure.TxRestDefaultConfigure;
import com.tairanchina.txRestFilter.engine.jersey.TxJerseyTools;

/**
 * Created by xujy on 15/11/24.
 */
@Provider
@PreMatching
public class MeetingFilter implements ContainerRequestFilter, ContainerResponseFilter
{
    private static Logger       logger         = LoggerFactory.getLogger(MeetingFilter.class);
    private static TxRestFilter requestFilter  = TxRestFilter.txJerseyRestFilter(
            new ElifeRequestFilterDefaultConfigure(), false);
    private static TxRestFilter responseFilter = TxRestFilter.txJerseyRestFilter(
            new ElifeResponseFilterDefaultConfigure(), true);

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException
    {
        requestFilter.processRequest(requestContext);
    }

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
            throws IOException
    {
        responseFilter.processResponse(requestContext, responseContext);
        MeetingConstants.clear();
    }

    /**
     * 请求过滤器的实现
     * Created by xujy on 15/11/24.
     */
    static class ElifeRequestFilterDefaultConfigure implements TxRestDefaultConfigure
    {
        @Override
        public boolean loadDefaultAuthFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultCustomHeaderFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultEchoFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultTickerFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultMaintenanceFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultQueryFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultXssSecurityFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultSQLSecurityFilter()
        {
            return false;
        }
    }

    /**
     * 响应过滤器的实现
     * Created by xujy on 15/11/24.
     */
    static class ElifeResponseFilterDefaultConfigure implements TxRestDefaultConfigure
    {
        @Override
        public boolean loadDefaultAuthFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultCustomHeaderFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultEchoFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultTickerFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultMaintenanceFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultQueryFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultXssSecurityFilter()
        {
            return false;
        }

        @Override
        public boolean loadDefaultSQLSecurityFilter()
        {
            return false;
        }
    }
}
